@echo off
cls

echo ==================================================
 echo          数据库远程连接配置检查工具            
 echo ==================================================

:: 1. 检查MySQL服务状态
echo [1/5] 检查MySQL服务状态...
sc query mysql | findstr "RUNNING"
if %errorLevel% EQU 0 (
    echo ✓ MySQL服务正在运行
) else (
    echo ✗ MySQL服务未运行，请先启动MySQL服务
)

echo.
:: 2. 检查远程用户是否创建
echo [2/5] 检查远程用户是否创建...
set /p root_password=请输入MySQL root用户密码: 

mysql -u root -p%root_password% -e "SELECT User, Host FROM mysql.user WHERE User = 'remote_user' AND Host = '%%';" > temp_user_check.txt

findstr /i "remote_user" temp_user_check.txt >nul
if %errorLevel% EQU 0 (
    echo ✓ 远程用户remote_user已创建
) else (
    echo ✗ 远程用户remote_user未创建
)
del temp_user_check.txt

echo.
:: 3. 检查MySQL配置
echo [3/5] 检查MySQL配置...
set /p mysql_path=请输入MySQL安装路径 [默认: C:\Program Files\MySQL\MySQL Server 8.0\]: 
if "%mysql_path%"=="" set mysql_path=C:\Program Files\MySQL\MySQL Server 8.0\

set config_file=%mysql_path%my.ini

findstr /i "bind-address = 0.0.0.0" "%config_file%" >nul
if %errorLevel% EQU 0 (
    echo ✓ MySQL配置正确，bind-address已设置为0.0.0.0
) else (
    echo ✗ MySQL配置不正确，bind-address未设置为0.0.0.0
    echo    请检查配置文件: %config_file%
)

echo.
:: 4. 检查防火墙规则
echo [4/5] 检查防火墙规则...
netsh advfirewall firewall show rule name="MySQL" >nul 2>&1
if %errorLevel% EQU 0 (
    echo ✓ 防火墙规则已添加
) else (
    echo ✗ 防火墙规则未添加
)

echo.
:: 5. 显示本机IP地址
echo [5/5] 显示本机IP地址...
ipconfig | findstr /i "IPv4"

echo.
echo ==================================================
 echo                    检查完成                      
 echo ==================================================
 echo 请确认所有检查项均已通过，如有未通过项，请重新配置
 echo 配置完成后，远程连接信息：
 echo 主机: 上面显示的IPv4地址
 echo 端口: 3306
 echo 用户名: remote_user
 echo 密码: remote_password123
 echo 数据库: community_service_platform
 echo ==================================================

pause